<?php

use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model app\models\Patient */
/* @var $form yii\widgets\ActiveForm */
?>

<style type="text/css">
    #arrive_doctor_html .oprate_doctor {
        text-align: center;
        margin-bottom: 5px;
        background-color: #eee;
    }
</style>

<div id="arrive_doctor_html" style="display: none;">
    <form lay-filter="oprate_form_id" class="layui-form layui-form-pane" style="width:260px;margin:20px;" method="post">
        <input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
        <?php foreach ($doctor_array as $d) { ?>
            <div class="layui-form-item oprate_doctor">
                <input class="guide_oprate_doctor_id" type="radio" name="patient[operate_doctor_id]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" />
            </div>
        <?php } ?>

        <div class="layui-form-item button" style="margin-top: 2em;">
            <button class="layui-btn" lay-submit lay-filter="oprateFormBeforeSubmit" style="width: 100%;">
                立即提交
            </button>
        </div>
    </form>
</div>

<script>
    window.yyh_arrive_doctor_html = document.getElementById('arrive_doctor_html');

    function patient_arrive(patient_name, patient_id) {
        $('#arrive_doctor_html').remove();
        layui.layer.open({
            id: 'arrive_doctor_html',
            title: '为患者 【' + patient_name + '】 选择接诊医生',
            area: ['300px', '500px'],
            type: 1,
            content: window.yyh_arrive_doctor_html.innerHTML, //这里content是一个普通的String
            success: function(layero, index) {
                layui.form.render();

                //监听提交
                layui.form.on('submit(oprateFormBeforeSubmit)', function(data) {
                    // console.log(data.field);
                    var doctor_name = $('input.guide_oprate_doctor_id:checked').attr('title');
                    layui.layer.confirm('为患者 【' + patient_name + '】 选择的接诊医生，确定是【' + doctor_name + '】吗?', {
                        icon: 3,
                        title: '再次确认'
                    }, function(index) {
                        var form_data = {};
                        form_data._csrf = '<?= Yii::$app->request->csrfToken ?>';
                        form_data['patient[operate_doctor_id]'] = $('input.guide_oprate_doctor_id:checked').val();
                        $.ajax({
                            url: "<?= Url::toRoute('patient/arrivedoctor') ?>&id=" + patient_id,
                            data: form_data,
                            dataType: 'json',
                            type: 'post',
                            async: false,
                            success: function(res) {
                                if (res.status == 'success') {
                                    layui.layer.msg('患者签到成功');
                                    return window.location.reload();
                                } else {
                                    layui.layer.msg('患者签到失败');
                                    console.log(res.error);
                                }

                            }
                        });
                        // layui.layer.close();
                    });

                    return false;
                });
            }
        })
    }
</script>